#!/bin/bash

# 检查Spring Boot应用状态脚本
echo "📊 Spring Boot应用状态检查"
echo "=========================="

# 检查进程
echo "🔍 进程检查:"
SPRING_PID=$(ps aux | grep "spring-boot:run" | grep -v grep | awk '{print $2}')
if [ -n "$SPRING_PID" ]; then
    echo "   ✅ Spring Boot进程运行中 (PID: $SPRING_PID)"
else
    echo "   ❌ 没有发现Spring Boot进程"
fi

# 检查端口
echo ""
echo "🔍 端口检查:"
if lsof -i:8081 >/dev/null 2>&1; then
    PORT_PID=$(lsof -ti:8081)
    echo "   ✅ 端口8081被占用 (PID: $PORT_PID)"
else
    echo "   ❌ 端口8081未被占用"
fi

# 检查API
echo ""
echo "🔍 API检查:"
if curl -s http://localhost:8081/api/monitor/machines >/dev/null 2>&1; then
    echo "   ✅ 机器API正常"
    MACHINE_COUNT=$(curl -s http://localhost:8081/api/monitor/machines | jq length 2>/dev/null || echo "未知")
    echo "      机器数量: $MACHINE_COUNT"
else
    echo "   ❌ 机器API无响应"
fi

if curl -s http://localhost:8081/api/monitor/pending-plans >/dev/null 2>&1; then
    echo "   ✅ 计划API正常"
    PLAN_COUNT=$(curl -s http://localhost:8081/api/monitor/pending-plans | jq length 2>/dev/null || echo "未知")
    echo "      计划数量: $PLAN_COUNT"
else
    echo "   ❌ 计划API无响应"
fi

# 检查日志文件
echo ""
echo "🔍 日志文件:"
if [ -f "app.log" ]; then
    echo "   ✅ 日志文件存在: app.log"
    echo "      最后10行日志:"
    tail -10 app.log | sed 's/^/      /'
else
    echo "   ❌ 日志文件不存在"
fi

echo ""
echo "📋 可用命令:"
echo "   ./start.sh    - 启动应用"
echo "   ./stop.sh     - 停止应用"
echo "   ./restart.sh  - 完整重启"
echo "   ./quick-restart.sh - 快速重启"
echo "   ./status.sh   - 检查状态" 